package com.atguigu.common.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;


@ApiModel(description = "通用的返回结果")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class R {
    @ApiModelProperty(value = "自定义返回状态码",example = "20000")
    private Integer code;//20000成功  20001失败
    private String message;//操作成功  操作失败
    private boolean success;//true false
    private Map<String,Object> data = new HashMap<>(); //存放数据

    public static R ok(){
        R result = new R();
        result.setCode(ResultCode.SUCCESS);
        result.setMessage("操作成功");
        result.setSuccess(true);
        return result;
    }


    public static R error(){
        R result = new R();
        result.setCode(ResultCode.ERROR);
        result.setMessage("操作失败");
        result.setSuccess(false);
        return result;
    }

    public R code(Integer code){
        this.setCode(code);
        return this;
    }
    public R success(boolean success){
        this.setSuccess(success);
        return this;
    }
    public R message(String message){
        this.setMessage(message);
        return this;
    }
    public R data(String key, Object value){
        this.data.put(key,value);
        return this;
    }

    public R data(Map<String,Object> data){
        this.setData(data);
//        this.data.putAll(data);
        return this;
    }

    public R dataAppend(Map<String,Object> data){
        this.data.putAll(data);
        return this;
    }


    public static void main(String[] args) {
        R result = R.ok();
        result.data("name","凯迪");
        System.out.println(result);
//        System.out.println(result);
//        Result result1 = result.code(12345);
//        System.out.println(result==result1);
//        System.out.println(result1);
    }

}
